package com.egg.ylt.presenter.impl;

import android.text.TextUtils;

import com.egg.ylt.Utils.API;
import com.egg.ylt.Utils.Constants;
import com.egg.ylt.pojo.BannerDataEntity;
import com.egg.ylt.pojo.CardDataEntity;
import com.egg.ylt.pojo.CouponEntity;
import com.egg.ylt.pojo.NatatoriumListPageEntity;
import com.egg.ylt.presenter.IHomePresenter;
import com.egg.ylt.view.IHomeView;
import com.yonyou.framework.library.bean.ErrorBean;
import com.yonyou.framework.library.common.CommonUtils;
import com.yonyou.framework.library.common.utils.ListUtil;
import com.yonyou.framework.library.mvp.impl.BasePresenter;
import com.yonyou.framework.library.net.ReqCallBack;
import com.yonyou.framework.library.net.RequestManager;

import java.util.HashMap;

/**
 * Created by lijiateng on 2019/1/17.
 */

public class HomePresenterImpl extends BasePresenter<IHomeView> implements IHomePresenter {

    public boolean isUserLogin() {
        return !TextUtils.isEmpty(Constants.TOKEN);
    }

    public void getCouponInfo() {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("token", Constants.TOKEN);
        RequestManager.getInstance().requestPostByAsyn(API.QUERY_REMIND_TICKET, hashMap, new ReqCallBack<CouponEntity>() {
            @Override
            public void onReqSuccess(CouponEntity result) {

                if (!ListUtil.isListEmpty(result.getList())) {
                    mView.showCouponDialog(result);
                }
            }

            @Override
            public void onFailure(String result) {

            }

            @Override
            public void onReqFailed(ErrorBean error) {

            }
        });
    }

    public void getHomeData() {

        // 获取 banner 列表
        getBannerData();

        // 获取次卡列表
        getCardData();

        // 获取门店列表
        getShopList("1");

    }

    private void getShopList(String pageNum) {

        mView.showLoading("正在加载中...");

        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("token", Constants.TOKEN);
        hashMap.put("pageSize", "20");
        hashMap.put("pageNum", pageNum);
        hashMap.put("companyId", Constants.COMPANYID);
        RequestManager.getInstance().requestPostByAsyn(API.GET_NATATORIUM_LIST_PAGE, hashMap, new ReqCallBack<NatatoriumListPageEntity>() {

            @Override
            public void onReqSuccess(NatatoriumListPageEntity result) {
                mView.hideLoading();
                mView.updateShopList(result.getList());
            }

            @Override
            public void onFailure(String result) {
                mView.hideLoading();
                CommonUtils.makeEventToast(mContext, result, false);
            }

            @Override
            public void onReqFailed(ErrorBean error) {
                mView.hideLoading();
                CommonUtils.makeEventToast(mContext, error.getMsg(), false);
            }
        });

    }

    private void getCardData() {

        mView.showLoading("正在加载中...");

        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("token", Constants.TOKEN);
        hashMap.put("companyId", Constants.COMPANYID);

        RequestManager.getInstance().requestPostByAsyn(API.GET_TIME_CARD_LIST, hashMap, new ReqCallBack<CardDataEntity>() {
            @Override
            public void onReqSuccess(CardDataEntity result) {
                mView.hideLoading();
                mView.updateCardList(result.getCardList());
            }

            @Override
            public void onFailure(String result) {
                mView.hideLoading();
            }

            @Override
            public void onReqFailed(ErrorBean error) {
                mView.hideLoading();
            }
        });

    }

    private void getBannerData() {

        mView.showLoading("正在加载中...");

        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("token", Constants.TOKEN);
        hashMap.put("companyId", Constants.COMPANYID);
        hashMap.put("type", "s");

        RequestManager.getInstance().requestGetByAsyn(API.GET_BANNER_DATA, hashMap, new ReqCallBack<BannerDataEntity>() {
            @Override
            public void onReqSuccess(BannerDataEntity result) {
                mView.hideLoading();
                mView.updateHomeBanner(result);
            }

            @Override
            public void onFailure(String result) {
                mView.hideLoading();
            }

            @Override
            public void onReqFailed(ErrorBean error) {
                mView.hideLoading();
            }
        });
    }


}
